<?php
/*
 * @Author: your name
 * @Date: 2020-10-27 14:27:00
 * @LastEditTime: 2021-01-04 14:22:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /special/application/adminapiv3/service/module_manage/disguise/DisguiseModuleRelationService.php
 */
namespace app\adminapiv3\service\module_manage\disguise;

use app\common\service\module_manage\disguise\HostGuiseConfig;
use app\adminapiv3\service\module_manage\ModuleRelationService;
use app\common\definition\disguise\DisguiseStatusDefinition;
use app\common\definition\InternetProtocolsDefinition;
use app\common\exception\CustomException;
use think\Db;

class DisguiseModuleRelationService extends ModuleRelationService
{
    public $module = 'disguise';
    /**
     * @name: 添加
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-10-27 14:28:21
     */
    public function add ()
    {
        $host_data = $this->getHost('os');
        $default_guise_port_a = $this->getAddDefaultGuisePortHost($host_data);
        $guise_config_a = $this->getAddGuiseConfigHost($host_data);

        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '主机开启系统伪装模块',
            'new_info' => '涉及主机:' . $host_name_s
        );

        Db::startTrans();
        try {
            Db::name('HostGuisePort')->insertAll($default_guise_port_a);
            Db::name('HostGuiseConfig')->insertAll($guise_config_a);
            Db::commit();
        } catch (\think\Exception $e)
        {
            Db::rollback();
            throw new CustomException('主机开启系统伪装失败:'. $e->getMessage(), 302);
        }
        
    }

    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-10-27 14:28:35
     */
    public function del ()
    {
        $host_id_a = $this->getHost('id');
        $HostGuiseConfig = new HostGuiseConfig();
        $HostGuiseConfig->onOff($host_id_a, DisguiseStatusDefinition::DISABLE);

        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '主机关闭系统伪装模块',
            'new_info' => '涉及主机:' . $host_name_s
        );
    }

    /**
     * @name: 冲突
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-10-28 14:17:59
     */
    public function conflictField (&$Host)
    {
        $Host->where('netfilter = 1 or hgshield = 1');
    }

    /**
     * @name: 冲突模块
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-10-28 15:23:03
     */
    public function conflictModule ()
    {
        return [];
    }

    /**
     * @name: 获取默认伪装端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-10 16:47:36
     */
    private function getAddDefaultGuisePortHost ($host_data)
    {
        $return = [];
        $host_id_a = array_keys($host_data);
        $disguise_port_host = Db::name('HostGuisePort')->whereIn('host_id', $host_id_a)->group('host_id')->column('id', 'host_id');
        
        foreach ($host_data as  $host_id => $os)
        {
            if(!isset($disguise_port_host[$host_id]))
            {
                $port = $this->defaultConfig($os);
            
                foreach ($port as $k => $v)
                {
                    $return[] = [
                        'host_id' => $host_id,
                        'port' => $v,
                        'protocols' => InternetProtocolsDefinition::TCP_PROTOCOL,
                        'status' => DisguiseStatusDefinition::DISABLE,
                        'type' => $k,
                    ];
                }
            }
            
        }
        
        return $return;
    }

    /**
     * @name: wundows 默认端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-07 15:11:09
     */
    private function defaultConfig ($os)
    {
        $return = [
            1 => [//WINDOWS
                'standard' => 3389 ,
                'standard' => 135 ,
                'sql' => 1521,
                'middleware' =>  9060
            ],
            2 => [//LINUX
                'standard' => 22 ,
                'standard' => 135 ,
                'sql' => 3306,
                'middleware' =>  1098
            ]
        ];

        return isset($return[$os]) ? $return[$os] : [] ;
    }

    /**
     * @name: 获取伪装需要添加配置的主机
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-10 17:03:35
     */
    private function getAddGuiseConfigHost ($host_data)
    {
        $return = [];
        $host_id_a = array_keys($host_data);
        $guise_config_a = Db::name('HostGuiseConfig')->whereIn('host_id', $host_id_a)->column('id','host_id');

        foreach ($host_id_a as $host_id)
        {
            if (!isset($guise_config_a[$host_id]))
            {
                $return[] = [
                    'host_id' => $host_id
                ];
            }
        }
        
        return $return;
    }
}